package com.bdc.whatwhat.activities;

import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.bdc.whatwhat.R;
import com.bdc.whatwhat.adapters.NotificationAdapter;
import com.bdc.whatwhat.adapters.ObjectAdapter;
import com.bdc.whatwhat.modele.Notification;
import com.bdc.whatwhat.modele.WhatWhat;
import com.bdc.whatwhat.session.SessionManager;
import com.bdc.whatwhat.task.FaireWhatWhatTask;
import com.bdc.whatwhat.task.MesNotificationsTask;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gfimac on 05/08/2014.
 */
public class NotificationsActivity extends SherlockActivity implements ObjectAdapter.ObjectAdapterLoadMore, MesNotificationsTask.MesNotificationsTaskDelegate, View.OnClickListener, NotificationAdapter.NotificationAdapterDelegate,FaireWhatWhatTask.FaireWhatWhatTaskDelegate {

    SessionManager sessionManager;
    View content;
    View aucune_notification;
    ListView listView;
    NotificationAdapter adapter;
    List<Notification> notifications = new ArrayList<Notification>();
    View progressBar;

    FaireWhatWhatTask faireWhatWhatTask;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        personnaliserActionBar();
        setContentView(R.layout.activity_notification);

        sessionManager = new SessionManager(this);

        charger();
        remplir();
        ajouterListeners();

        chargerNotifications();


        getSupportActionBar().setHomeButtonEnabled(true);


    }

    public void personnaliserActionBar() {

        getSupportActionBar().setIcon(getResources().getDrawable(R.drawable.ic_in_app));
        // Enable ActionBar app icon to behave as action to toggle nav drawer
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        try {
            int titleId = getResources().getIdentifier("action_bar_title", "id",
                    "android");
            TextView yourTextView = (TextView) findViewById(titleId);
            Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Pacifico.ttf");
            yourTextView.setTypeface(typeface);
            getSupportActionBar().setTitle(getResources().getString(R.string.notification));
        } catch (Exception e) {
        }
    }

    private void charger() {
        listView = (ListView) findViewById(android.R.id.list);
        content = findViewById(R.id.content);
        aucune_notification = findViewById(R.id.aucune_notification);
        progressBar = findViewById(R.id.progress_bar);

    }

    private void remplir() {
        adapter = new NotificationAdapter(this, notifications, this);
        listView.setAdapter(adapter);
    }

    private void ajouterListeners() {
        content.setOnClickListener(this);
    }


    public void chargerNotifications() {
        new MesNotificationsTask(this).execute(sessionManager.getToken());
    }

    public void reset() {
        notifications.clear();
        adapter.notifyDataSetChanged();
        ajouterListeners();
    }

    @Override
    public void loadMore() {

    }

    @Override
    public void faire(WhatWhat whatWhat) {
        if (faireWhatWhatTask != null)
            faireWhatWhatTask.cancel(true);
        faireWhatWhatTask = new FaireWhatWhatTask(this, FaireWhatWhatTask.EXISTANT);
        faireWhatWhatTask.execute(whatWhat, sessionManager.getToken());

    }

    @Override
    public void afficherChargement(boolean afficher) {
        if (afficher) {
            animerVues();
            progressBar.setVisibility(View.VISIBLE);
        }else {
            progressBar.setVisibility(View.GONE);
            progressBar.setAnimation(null);
        }

    }

    @Override
    public void onWhatWhatEnvoye(WhatWhat whatWhat) {
        supprimerToutLesDemandes();

    }

    @Override
    public void onResultatVide() {

    }

    @Override
    public void onMesNotificationsRecu(List<Notification> notifications) {
        reset();
        this.notifications.addAll(notifications);
        adapter.notifyDataSetChanged();

        if (this.notifications.size() == 0)
            aucune_notification.setVisibility(View.VISIBLE);

    }

    @Override
    public void onMesNotificationsVide() {
        if (this.notifications.size() == 0)
            aucune_notification.setVisibility(View.VISIBLE);
    }

    @Override
    public void onErreurReseau() {
        if (this.notifications.size() == 0)
            aucune_notification.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View view) {
        onBackPressed();
    }

    @Override
    public void supprimer(int position) {
        notifications.remove(position);
        adapter.notifyDataSetChanged();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // app icon in action bar clicked; goto parent activity.
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void animerVues() {

        final RotateAnimation rotate = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,
                0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

        rotate.setDuration(1000);
        rotate.setRepeatCount(Animation.INFINITE);
        rotate.setRepeatMode(Animation.RESTART);
        progressBar.startAnimation(rotate);

    }

    public void supprimerToutLesDemandes(){

        for(Notification notification : notifications)
            if(notification.getType() == Notification.DEMANDE_WHATWHAT)
                notifications.remove(notification);

        adapter.notifyDataSetChanged();

    }

}
